package com.allwees.bs.module.pms.vo;

import com.allwees.bs.core.modelbase.branch.EBranchType;
import com.allwees.bs.core.modelbase.importdata.ImportParam;
import com.allwees.bs.core.modelbase.req.Req;
import com.allwees.bs.core.modelbase.validate.ValidateItem;
import com.allwees.bs.module.pms.constant.ProductUploadTemplateEnum;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * @Author Daniel
 * @Version 1.0.0
 * @Description
 * @CreateTime 2020/8/15 18:04
 */
@Getter
@Setter
public class ImportProductReq extends Req implements ImportParam {
    /**
     * 上传的文件
     */
    private MultipartFile filedata;

    private ProductUploadTemplateEnum uploadExcelPlatform;

    private EBranchType branchType;
    /**
     * 是否自动提审
     */
    private boolean autoApply;
    /**
     * 错误时继续
     */
    private Boolean ignoreError;
    private String storeUuid;
    private String categoryNo;

    private Set<String> existSpuNos;
    private Set<String> existSkuNos;
    private Map<String, Object> addedProductMap = new HashMap<>();

    public void addResult(String key,Object value) {
        addedProductMap.put(key, value);
    }

    public Object getResult(String key) {
        return addedProductMap.get(key);
    }

    public Collection<Object> getResults(){
        return addedProductMap.values();
    }

    public boolean isCsv(){
        return StringUtils.endsWithIgnoreCase(filedata.getOriginalFilename(), ".csv");
    }

    public boolean isXls(){
        String fileName = filedata.getOriginalFilename();
        return StringUtils.endsWithIgnoreCase(fileName, ".xls") || StringUtils.endsWithIgnoreCase(fileName, ".xlsx");
    }


    @Override
    public ValidateItem[] getValidateItems() {
        return new ValidateItem[] {
                new ValidateItem(filedata == null, "文件不存在~"),
                new ValidateItem(filedata != null && filedata.isEmpty(), "文件内容为空~")
        };
    }
}
